package cn.tedu.dept;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;

@WebServlet("/DeptServlet")
public class DeptServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        String deptno = request.getParameter("deptno");
        String dname = request.getParameter("dname");
        String loc = request.getParameter("loc");
        System.out.println(deptno+dname+loc);

        //添加记录  增
       try {
           Class.forName("com.mysql.jdbc.Driver");
           String url = "jdbc:mysql:///gdb2109?characterEncoding=utf8";
           Connection con = DriverManager.getConnection(url, "root", "root");
           String sql = "insert into dept values (?,?,?)";
           PreparedStatement ps = con.prepareStatement(sql);
           ps.setString(1,deptno);
           ps.setString(2,dname);
           ps.setString(3,loc);
           ps.executeUpdate();

           ps.close();
           con.close();

       } catch (Exception e) {
           e.printStackTrace();
       }

       //查询记录
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url ="jdbc:mysql:///gdb2109?characterEncoding=utf8";
            Connection con = DriverManager.getConnection(url, "root", "root");
            String sql = "select * from dept where dname=? and loc= ?";
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setString(1,dname);
            ps.setString(2,loc);
            boolean execute = ps.execute();


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


    }
}
